package com.example.cardgame

//import android.widget.GridLayout
import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity

const val SCORE = "SCORE"
const val REQUEST_CODE_MODIFY = 1
const val REQUEST_CODE_GAME = 2
class MainActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        val modify = findViewById<android.widget.Button>(R.id.button_modify)
        modify.setOnClickListener {

            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_MODIFY)
        }
        val game = findViewById<android.widget.Button>(R.id.button_game)
        game.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)

            startActivityForResult(intent, REQUEST_CODE_GAME)

        }


    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
            REQUEST_CODE_MODIFY -> {
                if (resultCode == Activity.RESULT_OK) {
                    // 处理来自 SecondActivity 的结果
                    val name = data?.getStringExtra("name")
                    val xuehao = data?.getStringExtra("xuehao")
                    val phone = data?.getStringExtra("phone")
                    val gender = data?.getStringExtra("gender")
                    val imageByteArray = data?.getByteArrayExtra("image")

                    // 更新 UI
                    findViewById<TextView>(R.id.textView3).text = "姓名: $name"
                    findViewById<TextView>(R.id.textView5).text = "学号: $xuehao"
                    findViewById<TextView>(R.id.textView8).text = "电话: $phone"
                    findViewById<TextView>(R.id.textView4).text = "性别: $gender"
                    findViewById<TextView>(R.id.textView2).text = "$name+$xuehao"

                    imageByteArray?.let {
                        val bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
                        findViewById<ImageView>(R.id.imageView).setImageBitmap(bitmap)
                    }
                }
            }
            REQUEST_CODE_GAME -> {
                if (resultCode == Activity.RESULT_OK) {
                    // 处理来自 ThirdActivity 的结果
                    val score = data?.getStringExtra(SCORE)
                    val textView: TextView = findViewById(R.id.score)
                    textView.text = score
                }
            }
        }
    }


}
